<?php

namespace backend\modules\system\controllers;

use backend\extensions\ApiBaseController;
use backend\services\system\RoleService;

class RoleController extends ApiBaseController {
    
    /**
     * 列表
     * * */
    public function actionList() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['name'], 'default', 'value' => 0, 'message' => '名称检索'],
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
            [['page_size'], 'default', 'value' => 30, 'message' => '每页显示总数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = RoleService::getList($this->params);
        return $this->jsonSuccess($data);
    }
    
    /**
     * 新增
     * * */
    public function actionCreate() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['name','status'], 'required', 'message' => '{attribute}属必填项'],
            [['id','desc'], 'default', 'value' => '', 'message' => 'ID'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        if($this->params['id']){
            return RoleService::update($this->params);
        }
        unset($this->params['id']);
        return RoleService::create($this->params);
    }
    
    /**
     * 删除
     * * */
    public function actionDel() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return RoleService::delete($this->params);
    }
    
    /**
     * 启用/禁用
     * * */
    public function actionStatus() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id','status'], 'required', 'message' => '{attribute}属必填项'],
            [['status'], 'in', 'range' => [1, 2], 'message' => '{attribute}范围1~2'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return RoleService::update($this->params);
    }

    /**
     * 授权
     * * */
    public function actionAuth() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
            [['menu_id'], 'default', 'value' => '', 'message' => '默认参数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return RoleService::update($this->params);
    }

}
